class DialogMgr extends egret.Sprite{
    public static dmPool:Array<DialogMgr>=[];
    private bgMask:egret.Shape;
    private bg:egret.Shape;
    private txt:egret.TextField;
    constructor(){
        super();
    }
    public static show(msg:string){
        let stg:egret.Stage=egret.MainContext.instance.stage;
        let dm:DialogMgr=DialogMgr._new(msg);
        dm.txt.text=msg;
        stg.addChild(dm);
    }
    public onClick(e:egret.TouchEvent){
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
        if(this.parent)this.parent.removeChild(this);
        DialogMgr._free(this);
    }
    private static _create(msg:string){
        console.log("create dialog");
        let dm:DialogMgr=new DialogMgr();
        let stg:egret.Stage=egret.MainContext.instance.stage;
        dm.bgMask=DialogMgr.createShape(stg.stageWidth,stg.stageHeight,0x000000,0.5,0);
        dm.addChild(dm.bgMask);
        dm.bg=DialogMgr.createShape(400,260,0x000000,0.8,1,0xffffff,25);
        dm.bg.x=(stg.stageWidth-400)*0.5;
        dm.bg.y=(stg.stageHeight-260)*0.5;
        dm.addChild(dm.bg);
        dm.txt=UiHelper.createTxt({text:msg,width:400,height:250,x:dm.bg.x,y:dm.bg.y+10,align:"center",color:0xffffff,size:26,parent:dm});
        dm.touchEnabled=true;
        dm.addEventListener(egret.TouchEvent.TOUCH_TAP,dm.onClick,dm,true);
        return dm;
    }
    private static createShape(w:number,h:number,color=0x000000,alpha:number=1,border:number=1,borderColor=0xffffff,round:number=0){
        let sp:egret.Shape=new egret.Shape();
        sp.graphics.beginFill(color,alpha);
        sp.graphics.drawRoundRect(0,0,w,h,round,round);
        sp.graphics.endFill();
        sp.cacheAsBitmap=true;
        if(border==1){
            sp.graphics.lineStyle(border, borderColor, alpha);
            sp.graphics.drawRoundRect(0,0,w,h,round,round);
            sp.graphics.endFill();
        }
        return sp;
    }
    private static _new(msg:string){
        console.log("new");
        let dm:DialogMgr=null;
        dm=(DialogMgr.dmPool.length>0?DialogMgr.dmPool.pop():DialogMgr._create(msg));
        return dm;
    }
    private static _free(dm:DialogMgr){
        if(DialogMgr.dmPool.indexOf(dm)!=-1)return;
        console.log("free");
        DialogMgr.dmPool.push(dm);
    }
}